package com.breakfast.fun.ui.home.comment;

import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.user.Result;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yukai on 16/9/25.
 */

public class CommentPresenter extends CommentContract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    public void addComment(String orderId, String content, String productScore, String serviceScore, int orderType) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "comment");
        map.put("accountId", Constant.getUserData().getId() + "");
        map.put("orderId", orderId);
        map.put("content", content);
        map.put("tasteScore", productScore);
        map.put("speedScore", serviceScore);

        showLoadingDialog(true);
        if (orderType == 1) {
            rxManager.add(model.getAddMallCommentModel(map)
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .unsubscribeOn(Schedulers.io())
                    .subscribe(new Subscriber<Result>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            T.show("获取数据失败");
                            hideLoadingDialog();
                        }

                        @Override
                        public void onNext(Result result) {
                            hideLoadingDialog();
                            if (result.isSuccess()) {
                                view.showSuccess();
                            } else {
                                T.show(result.getMsg());
                            }
                        }
                    }));
        } else if (orderType == 0) {
            rxManager.add(model.getAddCommentModel(map)
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .unsubscribeOn(Schedulers.io())
                    .subscribe(new Subscriber<Result>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            T.show("获取数据失败");
                            hideLoadingDialog();
                        }

                        @Override
                        public void onNext(Result result) {
                            hideLoadingDialog();
                            if (result.isSuccess()) {
                                view.showSuccess();
                            } else {
                                T.show(result.getMsg());
                            }
                        }
                    }));
        }
    }
}
